/*
 *Copyright © 2015 zhaorongbao
 *招融宝
 *http://zhaorongbao.com
 *All rights reserved.
 */
package com.hanhai.zrb.api.biz.dao.mybatis;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.hanhai.zrb.api.biz.dao.mybatis.base.BaseMapper;
import com.hanhai.zrb.model.user.User;
import com.hanhai.zrb.model.user.dto.UserSearchDto;

public interface UserDao extends BaseMapper {
	
	User getLoginUser(@Param("umobile") String umobile,@Param("uname") String uname);

	@Insert("insert into user(uid,umobile,uemail,uname,password,ctime,org)values(#{uid},#{umobile},#{uemail},#{uname},#{password},now(),#{org})")
	void save(User user);

	@Select("select * from  user where uid =#{id}")
	User findById(Long id);

	@Update("update user set password = #{password},uptime=now() where uid = #{uid}")
	int updateByPwd(User user);
	
	@Update("update user set password = #{password},uptime=now() where umobile = #{umobile}")
	int updatePwdByMobile(User user);
	
	@Update("update user set password = #{password},uptime=now() where uid = #{uid}")
	int updateBy(User user);
	
	@Update("update user set org = #{org}, orgId=#{orgId} ,uptime=now() where uid = #{uid}")
	int updateOrg(User user);
	
	@Update("update user set status=12 ,org = #{org} ,orgId=#{orgId},authtime=NOW() , uptime=NOW() where uid = #{uid}")
	int updateForAuth(User user);

	void update(User user);
	

	User checkMobileOrEmailExist(@Param("mobile") String mobile,
			@Param("email") String email);

	/**
	 * 校验用户是否存在
	 */
	User checkUserNameExist(String name);

	/**
	 * 校验用户身份ID是否存在
	 */
	User checkUserIdcardExist(String idcard);

	/**
	 * 修改手机
	 */
	void updateModifyMobile(User user);

	@Select("select * from user where uname = #{uname} limit 0,1")
	User findByUname(String uname);

	@Select("select * from user where umobile = #{mobilephone} limit 0,1")
	User findByPhone(String mobilephone);

	@Update("update user set realname =#{real_name},idcard =#{card_id} ,uptime=now() where uid=#{uid}")
	void addCardInfo(@Param(value = "real_name") String real_name,
			@Param(value = "card_id") String card_id,
			@Param(value = "uid") String uid);

	@Select("select * from user where uemail = #{email}")
	User findByEmail(String email);

	@Update("update user set uemail = #{uemail} ,uptime=now() where uid = #{uid}")
	void updateEmail(User user);

	@Select("select * from user where uid = #{uid}")
	Map<String, Object> get(Long uid);

	List<Map<String, Object>> batchList(List<Map<String, Object>> list);

	List<User> listByKeyWord(UserSearchDto userSearch);
	
	List<User> findBySearch(UserSearchDto userSearch);
	
	List<User> findByKeyWord(UserSearchDto userSearch);
	
	List<User> queryUserBySearchDto(UserSearchDto userSearch);
	

	@Select("select * from user where idcard = #{idcard}")
	Map<String, Object> findCard(String idcard);

	List<User> findByUid(List<Long> list);

	@Update("update user set newbie = 0 ,uptime=now() where uid = #{uid}")
	void donotNewbie(Long uid);
	
	List<User> findByOrgIdAndUserType(@Param(value = "orgId")Long orgId,@Param(value = "type")Integer type);
	void upageStatus(@Param(value = "ids") List<Long> ids, @Param(value = "status") String status);
	@Select("select uid from user where realname like CONCAT('%', #{keyWord}, '%') OR umobile LIKE CONCAT(#{keyWord}, '%')")
	List<String> findUidByRealNameOrUmobile(String keyWord);
}
